package application.caiji.unitls;

import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.sql.RowSet;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

/**
 * @author ybt
 *
 */
public class JsonObjectUtil {


    public static JSONArray rowSetToJsonArray(RowSet rs) throws SQLException {
        JSONArray jsa = new JSONArray();
        if (rs != null) {
            ResultSetMetaData rsmd = rs.getMetaData();
            while (rs.next()) {
                JSONObject jsobj = new JSONObject();
                for (int i = 1; i <= rsmd.getColumnCount(); i++) {
                    if (rs.getString(rsmd.getColumnName(i)) == null) {
                        jsobj.put(rsmd.getColumnLabel(i), "");
                    } else {
                        jsobj.put(rsmd.getColumnLabel(i), rs.getString(rsmd.getColumnName(i)));
                    }

                }
                jsa.add(jsobj);
            }
        }
        return jsa;
    }

    public static JSONObject rowSetToJsonObject(RowSet rs) throws SQLException {
        JSONObject jsobj = new JSONObject();
        if (rs != null) {
            ResultSetMetaData rsmd = rs.getMetaData();
            if (rs.next()) {
                for (int i = 1; i <= rsmd.getColumnCount(); i++) {
                    if (rs.getString(rsmd.getColumnName(i)) == null) {
                        jsobj.put(rsmd.getColumnLabel(i), "");
                    } else {
                        jsobj.put(rsmd.getColumnLabel(i), rs.getString(rsmd.getColumnName(i)));
                    }
                }
            }
        }
        return jsobj;
    }
    
    /**
     * 实体类null处理
     * @param object
     * @return
     */
    public static String toJSONString(JSONObject object){
        return JSONObject.toJSONString(object, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullNumberAsZero, SerializerFeature.WriteNullListAsEmpty,SerializerFeature.WriteDateUseDateFormat);
    }
    
    /**
     * 将json字符串或键值对字符串转换为map
     * @param keyVStr
     * @param type json
     * @return
     */
    public static Map<String,String> keyStrToStrMap(String keyVStr,String type) {
		if(keyVStr==null) {
			return null;
		}
		if(type==null) {
			type="json";
		}
		Map<String,String> map=new HashMap<>();
    	if("json".equals(type)) {
    		JSONObject parseObject = JSONObject.parseObject(keyVStr);
    		Set<String> keySet = parseObject.keySet();
    		for (String key : keySet) {
    			map.put(key, parseObject.getString(key));	
			}
    	}else {
    		String[] split = keyVStr.split("\n");
    		for (int i = 0; i < split.length; i++) {
    			String[] split2 = split[i].split("：|:");
    			if(split2==null||split2.length!=2) {
    			}else {
    				map.put(split2[0].trim(),split2[1].trim());
    			}
			}
		}
    	return map;
	}
}
